package com.talentgame.tankspk.GameCore;

import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Event;
import com.badlogic.gdx.scenes.scene2d.EventListener;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonWriter;
import com.talentgame.tankspk.GameNet.DTO.PlayerModel;
import com.talentgame.tankspk.GameNet.DTO.PlayerShotDto;
import com.talentgame.tankspk.GameNet.ProtocolConst;
import com.talentgame.tankspk.GameNet.RequestPackage;

/**
 * Created by 54560 on 2016/12/26.
 */
public class Player {
    Tank tank;
    MyTouchPad leftTouchPad;
    MyTouchPad rigthTouchPad;
    public int score;
    float perShotSpan = 0.5f; //2秒射击1次
    float shotTimer=perShotSpan; //射击的计时器
    public int Hp = 50;
    PlayerModel lastPlayerInfo;
    public Player(Tank tank, MyTouchPad leftTouchPad, MyTouchPad rigthTouchPad) {
        this.tank = tank;
        this.leftTouchPad = leftTouchPad;
        this.rigthTouchPad = rigthTouchPad;
    }
    public void update(float detaltime)
    {
        boolean isShot  = false;
        shotTimer+= detaltime;

        ///如果这是玩家控制的角色
        //获取方向
        float x= leftTouchPad.getKnobPercentX();
        float y= leftTouchPad.getKnobPercentY();
        if(x !=0 || y != 0)
        {
            Vector3 direction = new Vector3(x,0,-y);
            //转换成摄像机方向
            direction.rotate(180,0,1,0);
            direction = direction.nor();
            tank.Move(direction,detaltime,true);
        }
//        rigthTouchPad.addListener(new InputListener()
//        {
//            @Override
//            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
//                super.touchUp(event, x, y, pointer, button);
//            }
//        });
        float rx = rigthTouchPad.getKnobPercentX();
        float ry = rigthTouchPad.getKnobPercentY();
        if(rx !=0 || ry != 0)
        {
            Vector3 direction = new Vector3(rx,0,ry);
            //转换成摄像机方向
            direction.rotate(180,0,1,0);
            direction = direction.nor();
            tank.setTurretLookAt(direction);
            if(shotTimer >= perShotSpan)
            {
                shotTimer = 0;
                tank.shotBullet();
                isShot= true;

            }
        }

        PlayerModel playerModel = tank.GetPlayerModel();
        playerModel.playerid = TanksGame.instance.playerid;
        //如果改变不是特别大，就不发送 数据包了,或者没有发射子弹
        if(!isChange(lastPlayerInfo,playerModel) && isShot == false)
            return;
        lastPlayerInfo = new PlayerModel(playerModel.playerid,playerModel.Hp,playerModel.state,
                playerModel.x,playerModel.y,playerModel.z,
                playerModel.tdx,playerModel.tdy,playerModel.tdz,
                playerModel.ldx,playerModel.ldy,playerModel.ldz);
        playerModel.isShot = isShot;

        Json json = new Json(JsonWriter.OutputType.json);
        RequestPackage rq= new RequestPackage(ProtocolConst.PlayerSync,json.toJson(playerModel));
        TanksGame.instance.client.sendRequest(rq);

    }

    private boolean isChange(PlayerModel a,PlayerModel b)
    {
        if(a == null || b== null)
            return true;
        boolean isMove = isChangeMove(a,b);
        boolean isLook = isChangeLook(a,b);
        boolean isRotation = isChangeRotation(a,b);
        if( !isMove&& !isLook && !isRotation)
        {
            return false;
        }
        return true;
    }
    private boolean isChangeMove(PlayerModel a,PlayerModel b)
    {
        float step = 0.01f;
        if(Math.abs(a.x - b.x) < step && Math.abs(a.y - b.y) < step &&Math.abs(a.z - b.z) < step)
        {
            b.isMove = false;
            return false;
        }
        b.isMove = true;
        return true;
    }
    private boolean isChangeLook(PlayerModel a,PlayerModel b){
        float step = 0.1f;
        if(Math.abs(a.ldx - b.ldx) < step &&Math.abs(a.ldy - b.ldy) < step &&Math.abs(a.ldz - b.ldz) < step)
        {
            b.isLook = false;
            return false;
        }
        b.isLook = true;
        return true;
    }

    private boolean isChangeRotation(PlayerModel a, PlayerModel b)
    {
        float step = 0.1f;
        if(Math.abs(a.tdx - b.tdx) < step &&Math.abs(a.tdy - b.tdy) < step &&Math.abs(a.tdz - b.tdz) < step )
        {
            b.isRotation=false;
            return false;
        }
        b.isRotation=true;
        return true;
    }
}
